package org.jsola.contract.helper;

import org.jsola.common.ClassKit;
import org.jsola.common.DateKit;
import org.jsola.common.SafeKit;
import org.jsola.contract.dto.PartySignAddDTO;
import org.jsola.contract.dto.PartySignUpdateDTO;
import org.jsola.contract.entity.PartySignDO;
import org.jsola.contract.query.PartySignQuery;
import org.jsola.contract.service.IPartySignService;
import org.jsola.contract.vo.PartySignVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

import static org.jsola.contract.core.AbstractTest.SITE_ID;



/**
 * 合同签署方签署记录
 *
 * @author JanusMix
 */
@Component
public class PartySignTestHelper extends BaseTestHelper {
    
    @Autowired
    private ContractTestHelper contractTestHelper;
    @Autowired
    private IPartySignService partySignService;

    /**
     * 生成add测试数据
     * @return PartySignAddDTO
     */
    public PartySignAddDTO getPartySignAddDTO(){
        PartySignAddDTO partySignAddDTO = new PartySignAddDTO();
        partySignAddDTO.setSignDetailUrl(getRandomValue("varchar",255));
        partySignAddDTO.setSignServiceId(getRandomValue("varchar",64));
        partySignAddDTO.setAccountId(getRandomValue("varchar",40));
        partySignAddDTO.setContractId(contractTestHelper.saveContractExample().getId());
        partySignAddDTO.setSealId(SafeKit.getLong(getRandomValue("bigint",11)));
        partySignAddDTO.setSealPositionId(SafeKit.getLong(getRandomValue("bigint",11)));
        partySignAddDTO.setSubjectId(getRandomValue("varchar",32));
        partySignAddDTO.setSignDate((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        partySignAddDTO.setStatus(SafeKit.getInteger(getRandomValue("tinyint",4)));
        return partySignAddDTO;
    }


    /**
     * 生成update测试数据
     * @return PartySignUpdateDTO
     */
    public PartySignUpdateDTO getPartySignUpdateDTO(PartySignVO partySignVO){
        return partySignService.selectDOById(partySignVO.getId(), SITE_ID).to(PartySignUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return PartySignQuery
     */
    public PartySignQuery getPartySignQuery() {
        PartySignQuery partySignQuery = new PartySignQuery();
        List<PartySignDO> partySignDOList = partySignService.list(new PartySignDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(partySignDOList)) {
            PartySignDO partySignDO = partySignDOList.get(0);
            partySignQuery = partySignDO.to(PartySignQuery.class);
            if (partySignDO.getSignDate() != null) {
                partySignQuery.setSignDateStart(DateKit.getBeforeDay(partySignDO.getSignDate(),-1));
                partySignQuery.setSignDateEnd(DateKit.getBeforeDay(partySignDO.getSignDate(),1));
            }
        }
        return partySignQuery;
    }

    /**
     * 保存示例
     * @return PartySignVO
     */
    public PartySignVO savePartySignExample(){
        PartySignAddDTO addDTO = this.getPartySignAddDTO();
        PartySignVO partySignVO = partySignService.save(addDTO,buildTokenUser());
        return partySignVO;
    }


}

